package com.company.project.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ComparatorChain;

public class SortUtil
{
	public static void main(String[] args)
	{
		System.out.println(testMapSort());
	}
	
	public enum OrderType
	{
		asc() , desc();
		OrderType(){};
	}


	/**
	 * 对list进行排序 asc
	 * 
	 * @param sortList  需要排序的list
	 * @param param   排序的参数名称
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<?> sort(List<?> sortList, String param )
	{
		
		return sort(sortList , param , OrderType.asc);
	}
	
	
	
	/**
	 * 对list进行排序
	 * 
	 * @param sortList  需要排序的list
	 * @param param1    排序的参数名称
	 * @param orderType  排序类型：正序-asc；倒序-desc
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<?> sort(List<?> sortList, String param, OrderType orderType)
	{
		Comparator<?> mycmp = ComparableComparator.getInstance();
		if (OrderType.desc.equals(orderType))
		{
			mycmp = ComparatorUtils.reversedComparator(mycmp); // 逆序（默认为正序）
		}

		ArrayList<Object> sortFields = new ArrayList<Object>();
		sortFields.add(new BeanComparator(param, mycmp)); // 主排序（第一排序）

		ComparatorChain multiSort = new ComparatorChain(sortFields);
		Collections.sort(sortList, multiSort);

		return sortList;
	}
	

	/**
	 * 对list进行排序 asc
	 * 
	 * @param sortList            需要排序的list
	 * @param firstParam          排序的参数名称:参数长度
	 * @param secondParam         排序的参数名称:排序参数
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List sortParam(List sortList, String firstParam, String secondParam)
	{
		return sortParam(sortList , firstParam , secondParam , OrderType.asc );
	}
	

	/**
	 * 对list进行排序
	 * 
	 * @param sortList        需要排序的list
	 * @param firstParam      排序的参数名称:参数长度
	 * @param secondParam     排序的参数名称:排序参数
	 * @param orderType       排序类型：正序-asc；倒序-desc
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List sortParam(List sortList, String firstParam, String secondParam, OrderType orderType)
	{
		Comparator mycmp1 = ComparableComparator.getInstance();
		Comparator mycmp2 = ComparableComparator.getInstance();
		if (OrderType.desc.equals(orderType))
		{
			mycmp1 = ComparatorUtils.reversedComparator(mycmp1); // 逆序(默认为正序)
		}

		ArrayList<Object> sortFields = new ArrayList<Object>();
		sortFields.add(new BeanComparator(firstParam, mycmp1)); // 主排序(第一排序)
		sortFields.add(new BeanComparator(secondParam, mycmp2)); //(第二排序)

		ComparatorChain multiSort = new ComparatorChain(sortFields);
		Collections.sort(sortList, multiSort);

		return sortList;
	}

	public static List testMapSort()
	{
		List sortList = new ArrayList();

		Map map1 = new HashMap();
		map1.put("name", "1");
		map1.put("age", "1");

		Map map2 = new HashMap();
		map2.put("name", "2");
		map2.put("age", "13");

		Map map3 = new HashMap();
		map3.put("name", "3");
		map3.put("age", "12");
		
		Map map5 = new HashMap();
		map5.put("name", "3");
		map5.put("age", "1");

		List list = new ArrayList();
		list.add(map3);
		list.add(map1);
		list.add(map2);
		list.add(map5);

		// return sort(list, "age", "asc");
		return sortParam(list, "name", "age", OrderType.desc);
	}

}
